package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyBaseQuery;
import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.model.TbyEducationSquare;
import com.zl.tby.repository.TbyEducationSquareRepo;
import com.zl.tby.service.ITbyEducationSquareService;
import com.zl.tby.vo.TbyEducationSquareForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 科研宣教-自然教育广场
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyEducationSquareServiceImpl implements ITbyEducationSquareService {

    @Db
    private TbyEducationSquareRepo tbyEducationSquareRepo;

    @Override
    @Tran
    public R<TbyEducationSquare> add(TbyEducationSquareForm form) {
        TbyEducationSquare entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyEducationSquareRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyEducationSquare> update(TbyEducationSquareForm form) {
        TbyEducationSquare entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyEducationSquareRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        int flag = tbyEducationSquareRepo.deleteById(id);
        return flag > 0 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyEducationSquare get(Long id) {
        return tbyEducationSquareRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyEducationSquare> page(TbyBaseQuery query) {
        IPage<TbyEducationSquare> iPage = tbyEducationSquareRepo.selectPage(query.getOffset(), query.getSize(), mp -> {});
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyEducationSquare> list() {
        return tbyEducationSquareRepo.selectList(mapperWhereQ -> {});
    }
}
